﻿var vm = this.lookupController().getViewModel();

var win = Ext.create('Ext.window.Window', {
    title: 'Choose Agency',
    height: 600,
    width: 900,
    modal: true,
    layout: 'fit',
    buttons: [{
        xtype: "button",
        text: "Confirm",
        handler: function () {
            var win = this.up("window"),
                refs = win.getReferences();
            //赋值
            var record = refs.datagrid1.getSelection();
            if (record.length > 0) {
                record = record[0];
                var agencyInfo = {};

                //第三方人员信息
                agencyInfo.chooseAgencyName = record.get('agencyName');
                agencyInfo.agencyName = record.get('agencyName');
                agencyInfo.mobilePhone = record.get('mobilePhone');
                agencyInfo.serviceEnd = record.get('serviceEnd');
                agencyInfo.agencyCDSID = record.get('approverCDSID');

                vm.set('NebulogyForm.agencyInfo', agencyInfo);
                vm.set('NebulogyForm.agencyData', record.data);
                vm.set('NebulogyForm.agencyBoardingId', record.get('id'));

                Ext.PAIRest.request({
                    ns: PAI.application.serviceUrl,
                    url: 'jlr-rest/api/jlrHrmResourceHis',
                    method: "post",
                    async: false,
                    data: {
                        personalID: record.get('personalID'),
                        pushStatus: 1,
                        showHistoryData: 0
                    },
                    success: function (records) {
                        vm.set('NebulogyForm.accessCardIsHave', records.accessCardIsHave);
                        vm.set('NebulogyForm.accountIsHave', record.accountIsHave);
                        vm.set('NebulogyForm.cdsidIsHave', record.cdsidIsHave);
                        vm.set('NebulogyForm.lockerIsHave', record.lockerIsHave);

                        var companyResourcesValue = {
                            accessCard: records.accessCardIsHave ? 1 : 0,
                            locker: records.lockerIsHave ? 1 : 0,
                            it: record.cdsidIsHave ? 1 : 0
                        }
                        vm.set('NebulogyForm.returnReminder.companyResources', companyResourcesValue);
                    }
                });

                Ext.PAIRest.request({
                    ns: PAI.application.serviceUrl,
                    url: 'jlr-rest/api/jlrHrmWorkLocationHis',
                    method: "post",
                    async: false,
                    data: {
                        personalID: record.get('personalID'),
                        pushStatus: 1
                    },
                    success: function (records) {

                        var jlr = getRoleName(records.jlrOfficeStr);
                        var ware = getRoleName(records.wareHouseStr);

                        function getRoleName(arr) {
                            var roleList = [];
                            Ext.each(arr, function (item) {
                                switch (item) {
                                    case 'Shanghai New Bund Office':
                                        roleList.push('NBAccessCard');
                                        break;
                                    case 'Shanghai Jinqiao':
                                        roleList.push('JQAccessCard');
                                        break;
                                    case 'RDC Warehouse':
                                    case 'RDC-JLR-Office(The Second Floor)':
                                        roleList.push('RDCAccessCard');
                                        break;
                                    case 'Beijing Office':
                                    case 'Beijing Academy':
                                        roleList.push('BJAccessCard');
                                        break;
                                    case 'Chengdu Office':
                                        roleList.push('CDAccessCard');
                                        break;
                                    case 'Guangzhou Office':
                                    case 'Guangzhou Academy':
                                        roleList.push('GZAccessCard');
                                        break;
                                }
                            });
                            return roleList;
                        }

                        vm.set('NebulogyForm.roleName', '"' + Array.from(new Set(jlr.concat(ware))).join('","') + '"');
                    }
                });


            }
            win.close();
        }
    }, {
        xtype: "button",
        text: "Cancel",
        handler: function () {
            this.up("window").close();
        }
    }],
    //主视图内部控件
    referenceHolder: true,
    items: [{
        xtype: "paigrid",
        mobileDisplay: true,
        hidden: false,
        readOnly: false,
        readOnlyHead: true,
        name: "datagrid1",
        editable: false,
        enableRowStatus: false,
        enableAdd: false,
        enableActions: true,
        reference: "datagrid1",
        enableSearch: false,
        enableDel: false,
        dockedItems: [{
            xtype: 'toolbar',
            dock: 'top',
            items: ['->', {
                xtype: 'textfield',
                emptyText: 'Agency Name',
                width: 300,
                listeners: {
                    blur: function (field) {
                        var searchWord = field.getValue().toLowerCase();
                        var store = win.down('grid').store;

                        if (searchWord) {
                            store.filterBy(function (record) {
                                return record.get('agencyName').indexOf(searchWord) != -1 || record.get('departmentHead').indexOf(searchWord) != -1;
                            });
                        } else {
                            store.clearFilter();
                        }
                    }
                }
            }]
        }],
        columns: [{
            text: language("Service Type"),
            dataIndex: "serviceType",
            minWidth: 100,
            flex: 1,
            renderer: function (value) {
                var str = '';
                switch (value) {
                    case '1': str = 'Short-term Project'; break;
                    case '2': str = 'Long-term Outsourcing Service'; break;
                    case '3': str = 'Short-term Replacement for LOA Employee'; break;
                    case '4': str = 'JV Partner'; break;
                    case '5': str = 'Guest'; break;
                    case '6': str = 'Intern'; break;
                }
                return str;
            }
        }, {
            text: language("Agency Name"),
            dataIndex: "agencyName",
            minWidth: 100,
            flex: 1
        }, {
            text: language("CDSID"),
            dataIndex: "approverCDSID",
            minWidth: 100,
            flex: 1
        }],
        store: {
            type: 'store',
            autoLoad: true,
            fields: [
                { name: "serviceType" },
                { name: "agencyName" },
                { name: "approverCDSID" }
            ],
            proxy: {
                type: 'pai.rest',
                ns: PAI.application.url,
                requestName: 'page',
                url: "last/hrm_agencyBoarding",
                reader: {
                    rootProperty: "hrm_agencyBoardingList"
                }
            },
            listeners: {
                beforeload: function (store) {
                    var proxy = store.getProxy();

                    proxy.setExtraParams({
                        pushStatus: 1
                    });
                }
            }
        }
    }],
    listeners: {
        close: function () {
            Ext.getBody().unmask();
        }
    }
}).show();